<?php

/*
    Testing utilities for the framework
    
        Author: Alfredo Mungo
        License: LGPL
        Copyright: 2012
*/

error_reporting(E_ALL);
ini_set('display_errors', '1');

$test_funcs = array();
$test_report_results = array();
$test_reports = array();

//Register a function for testing
function test_register($func, $name = null)
{
    global $test_funcs;
    $test_funcs[] = array($name, $func);
}


//Run the registered functions
function test_run()
{
    global $test_funcs, $test_reports, $test_report_results;
    foreach ($test_funcs as $test){
        $test_report_results = array();
        call_user_func($test[1]);
        $test_reports[] = $test_report_results;
    }
}


//INTERNAL, DO NOT USE
function test_report_init($idx)
{
    global $test_funcs;
    $tname = $test_funcs[$idx][0];
    
    if($tname != null)
        echo "<h3>$tname</h3>";
    
    echo '<div class="phi-test-indent">';
    echo '<table class="phi-test-table">';
    echo '<thead><th><b>Test description</b></th><th><b>Result</b></th></thead>';
    echo '<tbody>';
}

//INTERNAL, DO NOT USE
function test_report_end()
{
    echo '</tbody></table></div>';
}

//Add a result to a test report: $result is a boolean value which describes whether the test is successful.
function test_report_add_result($name, $result, $description = null)
{
    global $test_report_results;
    $test_report_results[] = array($name, $result, $description);
}

//Print a test report
function test_report_print()
{
    global $test_reports, $test_report_results;
    
    foreach($test_reports as $idx => $trep) {
        $trr = $trep;
        test_report_init($idx);
    
        foreach($trr as $test) {
            $tname = $test[0];
            $tres = $test[1];
            $desc = $test[2];
            
            echo "<tr" . ($desc != null? " title=\"$desc\"": "") . "><td>$tname</td><td " . ($tres? "data-success": "data-fail") . ">" . ($tres? "Success": "FAIL") . "</td></tr>\n";
        }
        
        test_report_end();
    }
}

function test_report_include_css()
{
    return '<link rel="stylesheet" type="text/css" href="test_suit.css">';
}

?>